package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.ZjSysContract;
import com.ruoyi.system.domain.vo.ZjSysContractVo;
import com.ruoyi.system.mapper.ZjSysContractMapper;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsWDetail;
import com.ruoyi.zhgd.domain.ZjOmnipotentRequires;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWReportBo;
import com.ruoyi.zhgd.domain.bo.WGroupBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWReportDetailVo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningUsingVo;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsWDetailMapper;
import com.ruoyi.zhgd.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWGroupBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWGroupVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsWGroup;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsWGroupMapper;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 万能机主表信息Service业务层处理
 *
 * @author tll
 * @date 2023-09-15
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
@Slf4j
public class NLaboratoryMechanicsWGroupServiceImpl implements INLaboratoryMechanicsWGroupService {

    private final NLaboratoryMechanicsWGroupMapper baseMapper;
    private final NLaboratoryMechanicsWDetailMapper detailMapper;
    @Autowired
    private IZjOmnipotentRequiresService requiresService;

    @Autowired
    private INLaboratoryMechanicsWDetailService wDetailService;
    @Autowired
    private IZjWarningSyService warningSyService;

    @Autowired
    private IZjNWarningUserMessageService messageService;

    @Autowired
    private IZjNWarningUsingService usingService;

    private final ZjSysContractMapper zjSysContractMapper;


    /**
     * 查询万能机主表信息
     */
    @Override
    public NLaboratoryMechanicsWGroupVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机主表信息列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsWGroupVo> queryPageList(NLaboratoryMechanicsWGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsWGroup> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsWGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机主表信息列表
     */
    @Override
    public List<NLaboratoryMechanicsWGroupVo> queryList(NLaboratoryMechanicsWGroupBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsWGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsWGroup> buildQueryWrapper(NLaboratoryMechanicsWGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsWGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSourceId() != null, NLaboratoryMechanicsWGroup::getSourceId, bo.getSourceId());
        lqw.eq(StringUtils.isNotBlank(bo.getSYLX()), NLaboratoryMechanicsWGroup::getSYLX, bo.getSYLX());
        lqw.eq(StringUtils.isNotBlank(bo.getSYBH()), NLaboratoryMechanicsWGroup::getSYBH, bo.getSYBH());
        lqw.eq(bo.getSYSL() != null, NLaboratoryMechanicsWGroup::getSYSL, bo.getSYSL());
        lqw.eq(bo.getGCZJ() != null, NLaboratoryMechanicsWGroup::getGCZJ, bo.getGCZJ());
        lqw.eq(bo.getArea() != null, NLaboratoryMechanicsWGroup::getArea, bo.getArea());
        lqw.eq(bo.getLo() != null, NLaboratoryMechanicsWGroup::getLo, bo.getLo());
        lqw.eq(bo.getSYSJ() != null, NLaboratoryMechanicsWGroup::getSYSJ, bo.getSYSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getMachine()), NLaboratoryMechanicsWGroup::getMachine, bo.getMachine());
        lqw.eq(StringUtils.isNotBlank(bo.getTester()), NLaboratoryMechanicsWGroup::getTester, bo.getTester());
        lqw.eq(bo.getTag() != null, NLaboratoryMechanicsWGroup::getTag, bo.getTag());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryMechanicsWGroup::getType, bo.getType());
        lqw.eq(bo.getCreateDate() != null, NLaboratoryMechanicsWGroup::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceType()), NLaboratoryMechanicsWGroup::getSourceType, bo.getSourceType());
        return lqw;
    }

    /**
     * 新增万能机主表信息
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsWGroupBo bo) {
        NLaboratoryMechanicsWGroup add = BeanUtil.toBean(bo, NLaboratoryMechanicsWGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改万能机主表信息
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsWGroupBo bo) {
        NLaboratoryMechanicsWGroup update = BeanUtil.toBean(bo, NLaboratoryMechanicsWGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsWGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机主表信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 万能机设备信息列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsWGroupVo> page(NLaboratoryMechanicsWGroupBo bo, PageQuery pageQuery) {

        Page<NLaboratoryMechanicsWGroupVo> result = baseMapper.page(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void verifyEligible(Integer id) {
        log.info("verifyEligible入参ID：" + id);
        //默认不合格
        Integer eligible = 0;
        //预警信息
        String remark = "";
        //查询试验列表 万能机主数据
        NLaboratoryMechanicsWGroupVo vo = baseMapper.selectWGroupVo(id);
        if (vo == null) {
            return;
        }
        log.info("vo：" + JsonUtils.toJsonString(vo));

        //查询试验详情 万能机子表数据
        List<NLaboratoryMechanicsWDetail> wDetails = wDetailService.selectWDetail(id);
        //查询技术参数
        ZjOmnipotentRequires requires = requiresService.selectRequires(vo.getSymc());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //判断试验类型 RZDLGJ热轧带肋钢筋  RZGYGJ热轧光圆钢筋  GJJXLJ钢筋机械连接   GJHJ钢筋焊接
        if (vo.getSYLX().equals("GJJXLJ") || vo.getSYLX().equals("GJHJ")) {
            //判断技术参数是否为空；为空则不对该项指标比较；
            if (requires.getRm() != null) {
                for (NLaboratoryMechanicsWDetail wDetail : wDetails) {
                    if (wDetail.getRm() >= requires.getRm()) {
                        eligible = 1;
                    } else {
                        remark = vo.getDeptName() + "试验时间：" + sdf.format(vo.getSYSJ()) + "，万能材料试验机，试验名称：" + vo.getSymc() + "，试验编号：" + vo.getSYBH()
                            + "极限强度:" + wDetail.getRm() + "小于" + requires.getRm() + ",不合格。";
                        eligible = 0;
                        break;
                    }
                }
            } else {
                eligible = 1;
            }
        } else if (vo.getSYLX().equals("RZDLGJ") || vo.getSYLX().equals("GJYC") || vo.getSYLX().equals("RZGYGJ")) {
            for (NLaboratoryMechanicsWDetail wDetail : wDetails) {
                //如果技术参数指标为空，忽略该项技术指标校验；
                if ((requires.getRel() == null || wDetail.getReL() >= requires.getRel())
                    && (requires.getRm() == null || wDetail.getRm() >= requires.getRm())
                    && (wDetail.getAgt() == null || requires.getAgt() == null || wDetail.getAgt().intValue() == 0 || wDetail.getAgt() >= requires.getAgt())
                    && (requires.getQu() == null || (wDetail.getRm() / wDetail.getReL()) >= requires.getQu())
                    && (requires.getQq() == null || requires.getRel() == null || ((wDetail.getReL() / requires.getRel()) <= requires.getQq()))) {
                    eligible = 1;
                } else {
                    remark = vo.getDeptName() + "试验时间：" + sdf.format(vo.getSYSJ()) + "，万能材料试验机，试验名称：" + vo.getSymc() + "，试验编号：" + vo.getSYBH();
                    if (requires.getRm() != null && wDetail.getRm() < requires.getRm()) {
                        remark += "极限强度:" + wDetail.getRm() + "小于" + requires.getRm();
                    }
                    if (requires.getRel() != null && wDetail.getReL() < requires.getRel()) {
                        remark += "下屈服强度:" + wDetail.getReL() + "小于" + requires.getRel();
                    }
                    if (wDetail.getAgt() != null && requires.getAgt() != null && wDetail.getAgt().intValue() != 0 && wDetail.getAgt() < requires.getAgt()) {
                        remark += "最大力总伸长率:" + wDetail.getAgt() + "小于" + requires.getAgt();
                    }
                    if (requires.getQu() != null && (wDetail.getRm() / wDetail.getReL()) < requires.getQu()) {
                        remark += "强屈比:" + (wDetail.getRm() / wDetail.getReL()) + "小于" + requires.getQu();
                    }
                    if (requires.getQq() != null && requires.getRel() != null && (wDetail.getReL() / requires.getRel()) > requires.getQq()) {
                        remark += "屈屈比:" + (wDetail.getRm() / wDetail.getReL()) + "大于" + requires.getQu();
                    }
                    remark += ",不合格。";
//                    remark = vo.getDeptName()+"试验时间："+sdf.format(vo.getSYSJ())+"，万能材料试验机，试验名称："+vo.getSymc()+"，试验编号："+vo.getSYBH()+"不合格。";
                    eligible = 0;
                    break;
                }
            }
        }
//        else if (vo.getSYLX().equals("RZGYGJ")) {
//            for (NLaboratoryMechanicsWDetail wDetail : wDetails) {
//                if ((requires.getRel() == null || wDetail.getReL() >= requires.getRel())
//                    && (wDetail.getAgt() == null || requires.getAgt() == null || wDetail.getAgt().intValue() == 0 || wDetail.getAgt() >= requires.getAgt())
//                    && (requires.getRm() == null || wDetail.getRm() >= requires.getRm())
//                    && (requires.getQu() == null || ((wDetail.getRm() / wDetail.getReL()) >= requires.getQu()))
//                    && (requires.getRel() == null || requires.getQq() == null || ((wDetail.getReL() / requires.getRel()) <= requires.getQq()))) {
//                    eligible = 1;
//                } else {
//                    remark = vo.getDeptName() + "试验时间：" + sdf.format(vo.getSYSJ()) + "，万能材料试验机，试验名称：" + vo.getSymc() + "，试验编号：" + vo.getSYBH();
//                    if (requires.getRm() != null && wDetail.getRm() < requires.getRm()) {
//                        remark += "极限强度:" + wDetail.getRm() + "小于" + requires.getRm();
//                    }
//                    if (requires.getRel() != null && wDetail.getReL() < requires.getRel()) {
//                        remark += "下屈服强度:" + wDetail.getReL() + "小于" + requires.getRel();
//                    }
//                    if (wDetail.getAgt() != null && requires.getAgt() != null && wDetail.getAgt().intValue() == 0 && wDetail.getAgt() < requires.getAgt()) {
//                        remark += "最大力总伸长率:" + wDetail.getAgt() + "小于" + requires.getAgt();
//                    }
//                    if (requires.getQu() != null && (wDetail.getRm() / wDetail.getReL()) < requires.getQu()) {
//                        remark += "强屈比:" + (wDetail.getRm() / wDetail.getReL()) + "小于" + requires.getQu();
//                    }
//                    if (requires.getQq() != null && requires.getRel() != null && (wDetail.getReL() / requires.getRel()) > requires.getQq()) {
//                        remark += "屈屈比:" + (wDetail.getRm() / wDetail.getReL()) + "大于" + requires.getQu();
//                    }
//                    remark += ",不合格。";
////                    remark = vo.getDeptName()+"试验时间："+sdf.format(vo.getSYSJ())+"，万能材料试验机，试验名称："+vo.getSymc()+"，试验编号："+vo.getSYBH()+"不合格。";
//                    eligible = 0;
//                    break;
//                }
//            }
//        }
        //修改合格参数
        baseMapper.updateEligible(id, eligible);
        if (eligible == 0) {
            //查询短信开关是否开启；
            List<ZjNWarningUsingVo> warnintUsing = usingService.selectByWgroupId(id);
            if (warnintUsing == null || warnintUsing.size() == 0) {
                log.info("发送短信失败......");
                return;
            } else {
                ZjNWarningUsingVo zjNWarningUsingVo = warnintUsing.get(0);
                if (zjNWarningUsingVo.getUsingMsgSy() == 0) {
                    log.info("实验室短信开关未开启");
                    //保存预警信息
                    saveWarningSy(remark, id, 3,vo.getDeptId());
                    return;
                }
            }
            //发送预警信息
            Integer message = sendTextMessage(remark, vo.getDeptId());
            //保存预警信息
            saveWarningSy(remark, id, message,vo.getDeptId());
        }
    }



    private Integer sendTextMessage(String remark, String deptId) {
        //查询发送手机号
        String warnValLab = "2";
        List<String> receiveAddr = messageService.selectReceiveAddr(deptId, warnValLab);
        Integer message = 2;
        if (receiveAddr.size() > 0) {
            message = usingService.send(receiveAddr, remark);
        }
        return message;
    }

    private void saveWarningSy(String remark, Integer id, Integer message,String deptId) {
        String type = "0";
        warningSyService.saveWarningSy(remark, id,type,message,deptId);
    }


    /**
     *
     * @param id
     * @param symc ：包括三种，热轧带肋钢筋、钢筋焊接试验、钢筋机械连接试验
     * @return
     */
    @Override
    public R<List<NLaboratoryMechanicsWReportDetailVo>> getReportInfo(Long id,String symc) {

        NLaboratoryMechanicsWGroupVo vo = baseMapper.selectWGroupVoById(id);
        if (vo == null) {
            return R.fail("没查到万能机主表信息");
        }

        vo.setAgtstr("≥" + vo.getAgt());
        vo.setRelstr("≥" + vo.getRel());
        vo.setQqstr("≤" + vo.getQq());
        vo.setQustr("≥" + vo.getQu());
        vo.setRmstr("≥" + vo.getRm());
        List<NLaboratoryMechanicsWDetail> details = wDetailService.selectWDetail(id.intValue());
        List<NLaboratoryMechanicsWReportDetailVo> reportDetailVos = new ArrayList<>();
        if (reportDetailVos == null || details.size() == 0) {
            return R.fail("没查到万能机数据信息");
        }
        /**
         * 查询合同信息
         *
         */
        ZjSysContractVo contract = zjSysContractMapper.selectVoOne(new LambdaQueryWrapper<ZjSysContract>()
            .eq(ZjSysContract::getDeptId, vo.getDeptId()));
        int i = 0;
        for (NLaboratoryMechanicsWDetail detail : details) {
            i++;
            NLaboratoryMechanicsWReportDetailVo reportVo = new NLaboratoryMechanicsWReportDetailVo();
            BeanUtils.copyProperties(detail, reportVo);
            BeanUtils.copyProperties(vo, reportVo);
            if(!symc.equals("钢筋机械连接")){
                reportVo.setQu(String.format("%.2f", detail.getRm() / detail.getReL()));
                reportVo.setQq(String.format("%.2f", detail.getReL() / vo.getRel()));
            }
            reportVo.setAgt(detail.getAgt());
            reportVo.setFm(detail.getFm());
            reportVo.setRm(detail.getRm());
            reportVo.setEligiblestr(vo.getEligible() == null ? "未校验" : vo.getEligible() == 0 ?  "不符合" : "符合");
            reportVo.setDetailId(detail.getId());
            reportVo.setTestNo(i);
            reportVo.setConstructionCompany(contract == null ? "" : contract.getSgGroup());
            reportVo.setProjectName(contract == null ? "" : contract.getProjectName());
            reportVo.setTestingName(contract == null ? "" : contract.getJcGroup());
            //处理特殊字段；
            if (vo.getSYBH() != null) {
                reportVo.setReportNo(vo.getSYBH().replace("YP", "BG"));
                reportVo.setRecordNo(vo.getSYBH().replace("YP", "JL"));
            }
            //检测依据
            reportVo.setTestingBasis("GB/T 228.1-2021 GB/T 28900-2022 GB/T 1499.2-2018");
            //判定依据
            reportVo.setJudgmentBasis("GB/T 1499.2-2018");
            //样品信息；样品名称:热轧带肋钢筋HIRB400直径12: 样品编号: JWS4-YP-JJ-202310-032; 样品教量:2根55m; 取样时间:2023年10月24日;样品态:无锈他无肉眼
            String ypxx = "样品名称：" + reportVo.getSpecificationGrade() + "；样品编号：" + vo.getSYBH() + "；样品数量：" + vo.getSYSL() + "根；" + "取样时间：" +
                reportVo.getTestDate() + "；样品状态：无锈蚀、无肉眼可见缺陷。";
            reportVo.setSampleInfo(ypxx);
            reportDetailVos.add(reportVo);
        }
        return R.ok(reportDetailVos);

    }

    /**
     * 万能机报表列表
     * @param id
     * @param symc
     * @return
     */
    @Override
    public R<List<NLaboratoryMechanicsWReportDetailVo>> getReportInfos(Long id, String symc) {
        NLaboratoryMechanicsWGroupVo vo = baseMapper.selectWGroupVoById(id);
        if (vo == null) {
            return R.fail("没查到万能机主表信息");
        }
        vo.setAgtstr("≥" + vo.getAgt());
        vo.setRelstr("≥" + vo.getRel());
        vo.setQqstr("≤" + vo.getQq());
        vo.setQustr("≥" + vo.getQu());
        vo.setRmstr("≥" + vo.getRm());
        List<NLaboratoryMechanicsWDetail> details = wDetailService.selectWDetail(id.intValue());
        List<NLaboratoryMechanicsWReportDetailVo> reportDetailVos = new ArrayList<>();
        if (reportDetailVos == null || details.size() == 0) {
            return R.fail("没查到万能机数据信息");
        }
        for (NLaboratoryMechanicsWDetail detail : details) {
            NLaboratoryMechanicsWReportDetailVo reportVo = new NLaboratoryMechanicsWReportDetailVo();
            BeanUtils.copyProperties(detail, reportVo);
            BeanUtils.copyProperties(vo, reportVo);
            reportVo.setQu(String.format("%.2f", detail.getRm() / detail.getReL()));
            reportVo.setQq(String.format("%.2f", detail.getReL() / vo.getRel()));
            reportVo.setEligiblestr(vo.getEligible() == 1 ? "合格" : "不合格");
            reportVo.setDetailId(detail.getId());
            reportDetailVos.add(reportVo);
        }
        return R.ok(reportDetailVos);
    }

    @Override
    public R saveReport(NLaboratoryMechanicsWReportBo bo) {
        if (bo != null) {
            //保存主表数据；
            NLaboratoryMechanicsWGroup group = new NLaboratoryMechanicsWGroup();
            WGroupBo wGroup = bo.getGroup();
            if (wGroup != null) {
                BeanUtils.copyProperties(wGroup, group);
            }
            group.setUpdateTime(new Date());
            group.setId(bo.getId());
            baseMapper.updateById(group);
            //保存子表数据；
            if (bo.getList() != null) {
                List<NLaboratoryMechanicsWReportDetailVo> list = bo.getList().getList();
                if (list != null && list.size() != 0) {
                    List<NLaboratoryMechanicsWDetail> detailList = new ArrayList<>();
                    for (NLaboratoryMechanicsWReportDetailVo vo : list) {
                        if (vo.getFractureFeature() != null) {
                            NLaboratoryMechanicsWDetail detail = new NLaboratoryMechanicsWDetail();
                            detail.setId(vo.getDetailId());
                            detail.setFractureFeature(vo.getFractureFeature());
                            detailList.add(detail);
                        }
                    }
                    if (detailList.size() != 0) {
                        detailMapper.updateBatchById(detailList);
                    }
                }
            }
        }
        return R.ok();

    }

    /**
     * 钢筋焊接报表保存
     * @param bo
     * @return
     */
    @Override
    public R hjsaveReport(NLaboratoryMechanicsWReportBo bo) {
        if(bo!=null) {
            //保存主表数据；
            NLaboratoryMechanicsWGroup group = new NLaboratoryMechanicsWGroup();
            WGroupBo wGroup = bo.getGroup();
            if (wGroup != null) {
                Date now = new Date();
                group.setPosition(wGroup.getPosition());
                group.setTestDate(wGroup.getTestDate());
                group.setTestCondition(wGroup.getTestCondition());
                group.setManufacturer(wGroup.getManufacturer());
                group.setQuantity(wGroup.getQuantity());
                group.setEntryDate(wGroup.getEntryDate());
                group.setAnnotations(wGroup.getAnnotations());
                group.setWeldType(wGroup.getWeldType());
                group.setSpecificationGrade(wGroup.getSpecificationGrade());
                group.setArea(wGroup.getArea());
                group.setGCZJ(wGroup.getGczj() == null ? null : Float.valueOf(wGroup.getGczj().replaceAll("[^0-9\\.]","").trim()));
                group.setWeldingQualityEvaluation(wGroup.getWeldingQualityEvaluation());
                group.setInstrumentNamesNumbers(wGroup.getInstrumentNamesNumbers());
                group.setUpdateTime(now);
                group.setId(bo.getId());
                baseMapper.updateById(group);
                //保存子表数据；
                List<NLaboratoryMechanicsWDetail> detailList = new ArrayList<>();
                NLaboratoryMechanicsWDetail detail = new NLaboratoryMechanicsWDetail();
                //第一条
                detail.setId(bo.getDetailId1());
                detail.setWeldLength(wGroup.getWeldLength1() == null ? null : wGroup.getWeldLength1());
                detail.setFm(wGroup.getFm1() == null ? null : Float.valueOf(wGroup.getFm1().replaceAll("[^0-9\\.]","")));
                detail.setRm(wGroup.getRm1() == null ? null : Float.valueOf(wGroup.getRm1().replaceAll("[^0-9\\.]","")));
                detail.setDLXZ(wGroup.getDlxz1() == null ? null : wGroup.getDlxz1());
                detail.setFractureWeld(wGroup.getFractureWeld1() == null ? null : wGroup.getFractureWeld1());
                detail.setUpdateTime(now);
                detailList.add(detail);
                //第二条
                detail = new NLaboratoryMechanicsWDetail();
                detail.setId(bo.getDetailId2());
                detail.setWeldLength(wGroup.getWeldLength2() == null ? null : wGroup.getWeldLength2());
                detail.setFm(wGroup.getFm2() == null ? null : Float.valueOf(wGroup.getFm2().replaceAll("[^0-9\\.]","")));
                detail.setRm(wGroup.getRm2() == null ? null : Float.valueOf(wGroup.getRm2().replaceAll("[^0-9\\.]","")));
                detail.setDLXZ(wGroup.getDlxz2() == null ? null : wGroup.getDlxz2());
                detail.setFractureWeld(wGroup.getFractureWeld2() == null ? null : wGroup.getFractureWeld2());
                detail.setUpdateTime(now);
                detailList.add(detail);
                //第三条
                detail = new NLaboratoryMechanicsWDetail();
                detail.setId(bo.getDetailId3());
                detail.setWeldLength(wGroup.getWeldLength3() == null ? null : wGroup.getWeldLength3());
                detail.setFm(wGroup.getFm3() == null ? null : Float.valueOf(wGroup.getFm3().replaceAll("[^0-9\\.]","")));
                detail.setRm(wGroup.getRm3() == null ? null : Float.valueOf(wGroup.getRm3().replaceAll("[^0-9\\.]","")));
                detail.setDLXZ(wGroup.getDlxz3() == null ? null : wGroup.getDlxz3());
                detail.setFractureWeld(wGroup.getFractureWeld3() == null ? null : wGroup.getFractureWeld3());
                detail.setUpdateTime(now);
                detailList.add(detail);
                if (detailList.size() != 0) {
                    detailMapper.updateBatchById(detailList);
                }
            }
        }
        return R.ok();
    }

    @Override
    public R jxljsaveReport(NLaboratoryMechanicsWReportBo bo) {
        if (bo != null) {
            //保存主表数据；
            NLaboratoryMechanicsWGroup group = new NLaboratoryMechanicsWGroup();
            WGroupBo wGroup = bo.getGroup();
            if (wGroup != null) {
                BeanUtils.copyProperties(wGroup, group);
            }
            group.setUpdateTime(new Date());
            group.setId(bo.getId());
            baseMapper.updateById(group);
            //保存子表数据；
            if (bo.getList() != null) {
                List<NLaboratoryMechanicsWReportDetailVo> list = bo.getList().getList();
                if (list != null && list.size() != 0) {
                    List<NLaboratoryMechanicsWDetail> detailList = new ArrayList<>();
                    for (NLaboratoryMechanicsWReportDetailVo vo : list) {
                        if (vo.getDetailId() != null) {
                            NLaboratoryMechanicsWDetail detail = new NLaboratoryMechanicsWDetail();
                            detail.setId(vo.getDetailId());
                            detail.setFm(vo.getFm());
                            detail.setRm(vo.getRm());
                            detail.setDLXZ(vo.getDLXZ());
                            detail.setDLWZ(vo.getDLWZ());
                            detail.setUpdateTime(new Date());
                            detailList.add(detail);
                        }
                    }
                    if (detailList.size() != 0) {
                        detailMapper.updateBatchById(detailList);
                    }
                }
            }
        }
        return R.ok();

    }


    /**
     * 获取钢筋焊接报表数据；
     * @param id
     * @param symc
     * @return
     */
    @Override
    public R<List<NLaboratoryMechanicsWReportDetailVo>> gethjReportInfo(Long id, String symc) {
        NLaboratoryMechanicsWGroupVo vo = baseMapper.selectWGroupVoById(id);
        if (vo == null) {
            return R.fail("没查到万能机主表信息");
        }
        vo.setAgtstr("≥" + vo.getAgt());
        vo.setRelstr("≥" + vo.getRel());
        vo.setQqstr("≤" + vo.getQq());
        vo.setQustr("≥" + vo.getQu());
        vo.setRmstr("≥" + vo.getRm());
        List<NLaboratoryMechanicsWDetail> details = wDetailService.selectWDetail(id.intValue());
        List<NLaboratoryMechanicsWReportDetailVo> reportDetailVos = new ArrayList<>();
        if (reportDetailVos == null || details.size() == 0) {
            return R.fail("没查到万能机数据信息");
        }
        /**
         * 查询合同信息
         *
         */
        ZjSysContractVo contract = zjSysContractMapper.selectVoOne(new LambdaQueryWrapper<ZjSysContract>()
            .eq(ZjSysContract::getDeptId, vo.getDeptId()));
        NLaboratoryMechanicsWDetail detail1 = details.get(0);
        NLaboratoryMechanicsWDetail detail2 = details.get(1);
        NLaboratoryMechanicsWDetail detail3 = details.get(2);
        int i = 0;
        for (NLaboratoryMechanicsWDetail detail : details) {
            i++;
            NLaboratoryMechanicsWReportDetailVo reportVo = new NLaboratoryMechanicsWReportDetailVo();
            BeanUtils.copyProperties(detail, reportVo);
            BeanUtils.copyProperties(vo, reportVo);
            reportVo.setRm(detail.getRm());
            reportVo.setTestNo(i);
            reportVo.setEligiblestr(vo.getEligible() == null ? "未校验" : vo.getEligible() == 0 ?  "不符合" : "符合");
            reportVo.setDetailId(detail.getId());
            reportVo.setConstructionCompany(contract == null ? "" : contract.getSgGroup());
            reportVo.setProjectName(contract == null ? "" : contract.getProjectName());
            reportVo.setTestingName(contract == null ? "" : contract.getJcGroup());
            //第一条
            reportVo.setDetailId1(detail1.getId());
            reportVo.setRm1(detail1.getRm());
            reportVo.setFm1(detail1.getFm());
            reportVo.setWeldLength1(detail1.getWeldLength());
            reportVo.setDlxz1(detail1.getDLXZ());
            reportVo.setFractureWeld1(detail1.getFractureWeld());
            //第二条
            reportVo.setDetailId2(detail2.getId());
            reportVo.setRm2(detail2.getRm());
            reportVo.setFm2(detail2.getFm());
            reportVo.setWeldLength2(detail2.getWeldLength());
            reportVo.setDlxz2(detail2.getDLXZ());
            reportVo.setFractureWeld2(detail2.getFractureWeld());
            //第三条
            reportVo.setDetailId3(detail3.getId());
            reportVo.setRm3(detail3.getRm());
            reportVo.setFm3(detail3.getFm());
            reportVo.setWeldLength3(detail3.getWeldLength());
            reportVo.setDlxz3(detail3.getDLXZ());
            reportVo.setFractureWeld3(detail3.getFractureWeld());
            //处理特殊字段；
            if (vo.getSYBH() != null) {
                reportVo.setReportNo(vo.getSYBH().replace("YP", "BG"));
                reportVo.setRecordNo(vo.getSYBH().replace("YP", "JL"));
            }
            //检测依据
            reportVo.setTestingBasis("GB/T 228.1-2021 GB/T 28900-2022 GB/T 1499.2-2018");
            //判定依据
            reportVo.setJudgmentBasis("GB/T 1499.2-2018");
            //样品信息；样品名称:热轧带肋钢筋HIRB400直径12: 样品编号: JWS4-YP-JJ-202310-032; 样品教量:2根55m; 取样时间:2023年10月24日;样品态:无锈他无肉眼
            String ypxx = "样品名称：" + reportVo.getSpecificationGrade() + "；样品编号：" + vo.getSYBH() + "；样品数量：一组(3根)；" + "取样时间：" +
                reportVo.getTestDate() + "；样品状态：焊缝饱满、无裂纹。";
            reportVo.setSampleInfo(ypxx);
            reportDetailVos.add(reportVo);
        }
        return R.ok(reportDetailVos);
    }


}
